<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\Message as MessageModel;

use app\admin\validate\Message as MessageValidate;
use think\exception\ValidateException;

use think\facade\Request;
use think\facade\View;
use think\facade\Config;

/**
 * 留言管理
 * Class AdminUser
 * @package app\admin\controller
 */
class Message extends Base
{
 
    /**
     * 留言管理
     * @return mixed
     */
    public function index()
    {
        $message_list = MessageModel::select();
		View::assign('list',$message_list);

		
		return View::fetch('index');
    }

  
    /**
     * 编辑留言
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {

		if (Request::isPost()) {

			$data = Request::only(['id', 'name', 'tel', 'mobile', 'email', 'message', 'ip', 'status']);

            try {
				validate(MessageValidate::class)->scene('edit')->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (MessageModel::update($data, ['id' => $id])) {
				$this->success('更新成功');
			} else {
				$this->error('更新失败');
			}


        }


        $message	= MessageModel::find($id);

		View::assign('info',$message);
		

		return View::fetch('edit');
    }

    /**
     * 删除留言
     * @param $id
     */
    public function delete($id)
    {
        if (MessageModel::destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }


}